import { titleByLayerType } from '@/pages/TemplateEditor/constant';
import { LayerTypeBE, MAX_LAYER_NAME_LENGTH } from '../constant';

import { LayerInfoFE } from '@/pages/TemplateEditor/interface';

export const generateNewLayerName = (
  layerType: LayerTypeBE,
  currentLayers: LayerInfoFE[]
): string => {
  const currentLayerNumByType = currentLayers.filter(
    (layer) => layer.data.type === layerType
  ).length;

  return `${titleByLayerType[layerType]}${currentLayerNumByType + 1}`;
};

const getCopyLayerName = (
  oldName,
  index = 0,
  currentNames: string[]
): string => {
  let newName = index > 0 ? oldName + '_clone_' + index : oldName + '_clone';
  newName =
    newName.length > MAX_LAYER_NAME_LENGTH
      ? index > 0
        ? oldName.substr(0, 22) + '_clone_' + index
        : oldName.substr(0, 22) + '_clone'
      : newName;
  const isExist = currentNames.some((name) => {
    return name === newName;
  });
  if (isExist) {
    return getCopyLayerName(oldName, ++index, currentNames);
  } else {
    return newName;
  }
};

export const generateCopyLayerName = (
  oldName: string,
  currentNames: string[]
): string => {
  // 先找到当前 copy 的最大下标
  let maxIndex = -1;
  const tailLength = '_clone';
  const originNameMaxLength = MAX_LAYER_NAME_LENGTH - tailLength.length;
  currentNames.forEach((name) => {
    if (name && typeof name === 'string') {
      if (oldName.length >= originNameMaxLength) {
        oldName = oldName.substring(0, originNameMaxLength);
      }

      const nameArr = name.split(oldName + '_clone_');
      if (
        nameArr.length === 2 &&
        !isNaN(Number(nameArr[1])) &&
        Number(nameArr[1]) > maxIndex
      ) {
        maxIndex = Number(nameArr[1]);
      }
    }
  });

  return getCopyLayerName(oldName, ++maxIndex, currentNames);
};

export const componentToHex = (c): string => {
  const hex = c.toString(16);
  return hex.length == 1 ? '0' + hex : hex;
};

export const rgbToHex = (r, g, b): string => {
  return '#' + componentToHex(r) + componentToHex(g) + componentToHex(b);
};

export const hexToRgb = (hex: string): { r: number; g: number; b: number } => {
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result
    ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
      }
    : { r: 0, g: 0, b: 0 };
};

export const hexToRgba = (hex: string, transparency): string => {
  const { r, g, b } = hexToRgb(hex);

  return `rgba(${r}, ${g}, ${b}, ${transparency})`;
};

export const getRGBAColorFromArray = (
  fillColor: string,
  transparency: number
): string => {
  const { r, g, b } = hexToRgb(fillColor);

  return `rgba(${r}, ${g}, ${b}, ${transparency})`;
};

export const getRGBAColorFromString = (
  rgbaColor
): {
  fillColor: string;
  transparency: number;
} => {
  const rgbaColorArray = rgbaColor
    .substring(rgbaColor.indexOf('(') + 1, rgbaColor.lastIndexOf(')'))
    .split(/,\s*/);
  const [r, g, b, transparency] = rgbaColorArray;

  return {
    fillColor: rgbToHex(r, g, b),
    transparency
  };
};
